/**
 * @product: PCDC-CMS
 * @copyright: FSAI-南征北战信息科技
 * @filename: com.fsai.cms.controller.cms.CateController.java
 *
 * @link: Http://www.fastsai.com
 * @jdkv: Jdk 1.8
 */
package com.fsai.cms.controller.cms;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.fsai.cms.base.BaseResp;
import com.fsai.cms.base.WebCons;
import com.fsai.cms.base.Ztree;
import com.fsai.cms.config.CmsSiteConfig;
import com.fsai.cms.domain.BaseSiteCfg;
import com.fsai.cms.domain.CmsCate;
import com.fsai.cms.domain.CmsCont;
import com.fsai.cms.service.ICmsCateService;
import com.fsai.cms.service.ICmsContService;
import com.fsai.cms.service.ICmsPubService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 *
 * @project: pcdc-cms-web
 *
 * @author: JaffeHe
 * @email: JaffeHe@foxmail.com
 * @date: 2021-03-13 12:01
 *
 * @ChangeLog<br/>
 * 				Developer Date Comment <br/>
 */
@RestController
@RequestMapping("/cate")
@Api(tags = "栏目管理")
public class CateController {
	@Autowired
	private ICmsCateService cmsCateService;
	@Autowired
	private ICmsPubService cmsPubService;
	@Autowired
	private ICmsContService cmsContService;
	@Autowired
	private CmsSiteConfig siteCfg;

	@GetMapping("/stree/{orgId}")
	@ApiOperation("获取机构站点栏目单层树")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orgId", value = "所属处室", defaultValue = "1", required = true) })
	public BaseResp cateSTree(@PathVariable("orgId") String orgId) {
		List<Ztree> cmsCateTree = cmsCateService.selectCmsCateTree(orgId);

		return BaseResp.success(cmsCateTree);
	}

	@GetMapping("/nav/{orgId}")
	@ApiOperation("获取机构站点栏目列表")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orgId", value = "所属处室", defaultValue = "1", required = true) })
	public BaseResp nav(@PathVariable("orgId") String orgId) {
		List<CmsCate> cateList = cmsCateService.selectCateList(orgId);

		return BaseResp.success(cateList);
	}

	@GetMapping("/tree/{orgCode}")
	@ApiOperation("获取机构站点栏目多层树")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orgCode", value = "所属处室代码", defaultValue = "wjw", required = true) })
	public BaseResp cateMTree(@PathVariable("orgCode") String orgCode) {
		int topId = cmsPubService.findOrgTopCateId(orgCode);
		List<CmsCate> cateList = cmsCateService.selectCateListToTree(orgCode, topId);

		return BaseResp.success(cateList);
	}

	@GetMapping("/topnews/{cateCode}")
	@ApiOperation("获取栏目首页文章")
	@ApiImplicitParams({ @ApiImplicitParam(name = "cateCode", value = "栏目代码", required = true) })
	public BaseResp cateIndexNews(@PathVariable("cateCode") String cateCode) {
		//1.查询栏目信息
		CmsCate cmsCate = cmsCateService.queryCateByCode(cateCode);

		//2.查询栏目下文章列表
		List<CmsCont> news = cmsContService.selectTopNews(cmsCate.getCateId(), cmsCate.getRecomPageCount());

		return BaseResp.success(news);
	}

	@GetMapping("/pgdata/{cateId}/{pageSize}/{pageNum}")
	@ApiOperation("获取栏目首页文章")
	@ApiImplicitParams({ @ApiImplicitParam(name = "cateId", value = "栏目ID", required = true),
			@ApiImplicitParam(name = "pageSize", value = "每页数量", required = true),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true), })
	public BaseResp catePageNews(@PathVariable("cateId") String cateId, @PathVariable(name = "pageSize") int pageSize,
			@PathVariable("pageNum") int pageNum) {
		CmsCont query = new CmsCont();
		query.setCateId(Long.parseLong(cateId));
		query.setContDisplay(WebCons.STATUS_Y);
		query.setStatus(WebCons.STATUS_Y);
		//对于中心的信息公开栏目，可以将内设机构上传的文章同步中心的信息公开处，同步按钮由中心管理员进行操作
		//1判断是否主站公开栏目
		String spceCateId = cmsPubService.getSycConfVal("biz.main.speccateid");
		if (cateId.equals(spceCateId)) {//特殊站点
			query.setSpecCate("Y");
		}

		PageInfo<CmsCont> pageData = this.cmsContService.queryPageData(query, pageSize, pageNum);
		return BaseResp.success(pageData);
	}

	/**
	 * 栏目列表主页
	 * 
	 * @Developer: JaffeHe - Date:2021-03-14 14:58
	 * @DepEmail: JaffeHe@foxmail.com
	 *
	 * @param cateCode
	 * @param model
	 * @return
	 * @ChangeLog:<br/>
	 */
	@ApiIgnore
	@GetMapping("/index/{cateCode}")
	public ModelAndView index(@PathVariable("cateCode") String cateCode, Model model) {
		//1查询栏目信息
		CmsCate cmsCate = cmsCateService.queryCateByCode(cateCode);
		model.addAttribute("cmsCate", cmsCate);
		//2站点默认图片
		BaseSiteCfg siteInfo = cmsPubService.selSiteInfo(siteCfg.getMainId());
		model.addAttribute("siteInfo", siteInfo);

		return new ModelAndView("pcdc/catelist", "biz", model);
	}

	/**
	 * 子机构栏目列表主页
	 * 
	 * @Developer: JaffeHe - Date:2021-03-14 14:58
	 * @DepEmail: JaffeHe@foxmail.com
	 *
	 * @param cateCode
	 * @param model
	 * @return
	 * @ChangeLog:<br/>
	 */
	@ApiIgnore
	@GetMapping("/index/{orgCode}/{cateCode}")
	public ModelAndView sindex(@PathVariable("orgCode") String orgCode, @PathVariable("cateCode") String cateCode,
			Model model) {
		model.addAttribute("orgCode", orgCode);
		//1查询栏目信息
		CmsCate cmsCate = cmsCateService.queryCateByCode(cateCode);
		model.addAttribute("cmsCate", cmsCate);
		//2站点默认图片
		BaseSiteCfg siteInfo = cmsPubService.selSiteInfo(siteCfg.getMainId());
		model.addAttribute("siteInfo", siteInfo);

		return new ModelAndView("pcdc/subcatelist", "biz", model);
	}
}
